package com.laiyima.zhongjiang.linghuilv.demo.me;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.MainActivity;
import com.laiyima.zhongjiang.linghuilv.demo.R;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class ModifypwActivity extends SwipeBackActivity implements View.OnClickListener {
    private String TAG="ModifypwActivity";
    private int modifCode;
    private LinearLayout imageView;
    private EditText oldText,newText;
    private Button button;
    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modifypw);
        imageView= (LinearLayout) findViewById(R.id.uesr_modify_back);
        oldText= (EditText) findViewById(R.id.old_password);
        newText= (EditText) findViewById(R.id.new_password);
        button= (Button) findViewById(R.id.sava_password_button);

        setStatusBar();
        imageView.setOnClickListener(this);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.uesr_modify_back:
                finish();
                break;
            case R.id.sava_password_button:
                saveNewPass();
                break;

        }
    }

    private void saveNewPass() {
        String old=oldText.getText().toString();
        String mynew=newText.getText().toString();
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params1 = new RequestParams("http://zljl.laiima.com/index.php?s=/api/Register/edit_password");
        //需要带入的参数
        params1.addBodyParameter("uid", userID);
        params1.addBodyParameter("old_password", old);
        params1.addBodyParameter("new_password", mynew);
        // params.addBodyParameter("captcha", captcha);
        x.http().post(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e(TAG, "回调成功");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    modifCode = jsonObject.optInt("code", 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (modifCode == 200) {
                    Intent Nameintent=new Intent(ModifypwActivity.this, MainActivity.class);
                    startActivity(Nameintent);
                    Toast.makeText(getBaseContext().getApplicationContext(), "密码修改成功", Toast.LENGTH_SHORT).show();
                }
                if (modifCode == 201) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "输入不能为空", Toast.LENGTH_SHORT).show();
                }
                if (modifCode == 202) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "密码长度至少 6 位，必须包含有大写字母、小写字母、数字", Toast.LENGTH_SHORT).show();
                }
                if (modifCode == 203) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "旧密码错误", Toast.LENGTH_SHORT).show();
                }
                if (modifCode == 204) {

                    Toast.makeText(getBaseContext().getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                }
                if (modifCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "系统异常");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "查看实名认证失败");
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });


    }
}
